*Stata Code to replicate the main tables/figures of the paper: 

*Florackis C., Louca, C., Michaely, R. and & M. Weber (2023) Cybersecurity Risk, The Review of Financial Studies

*The file flmw_rfs.dta includes our firm-level measure of cybersecurity risk (Stata format). The variable sim_breaches_t_1_all is our cybersecurity risk measure (cosine similarity)

*Table 1 can be directly replicated by checking  key statistics of the measure (highest scores/ low scores etc) 

*Figure 1/ Cybersecurity Risk by Year
use "C:\sample_final_clean.dta"
global depvar sim_breaches_t_1_all
global controls bm size lnsize age lnage tobinsq roa tangibility xrd_at secrets5 ind_std_cfo_at risk_length   lnrisk_length   read  lnread inst_perblock ind_directors  risk_com 

winsor2 $controls, suffix(_w) cuts(1 99) by(fyear)
egen cyber_risk_mean_year = mean(sim_breaches_t_1_all), by(fyear)
egen tag_year = tag(fyear)
twoway connected cyber_risk_mean_year fyear if tag_year, sort
* stats by year 
set more off
eststo clear 
sort fyear
by fyear: eststo: quietly estpost summarize sim_breaches_t_1_all
esttab using "C:\by_year.csv", cells("mean") label nodepvar  replace
*stats by industry 
set more off
eststo clear
sort ind_ff12
by ind_ff12: eststo: quietly estpost summarize sim_breaches_t_1_all
esttab using "C:\by_industry.csv", cells("mean") label nodepvar  replace

*Table 2 Correlations
gen direct_indirect_ratio= direct_indirect_s/ ( direct_indirect_s+ risk_length)
winsor2 sent_neg2 sent_lit2  sent_uncert2 direct_indirect_s  direct_indirect_ratio    , suffix(_w) cuts(1 99)
gen precise_w= - sent_uncert2_w
pwcorr sim_breaches_t_1_all  direct_indirect_s  direct_indirect_ratio  sent_neg2_w precise_w sent_lit2_w    insurance, sig star(0.01)
pwcorr sim_breaches_t_1_all  direct_indirect_s  direct_indirect_ratio  sent_neg2_w precise_w sent_lit2_w  insurance if sim_breaches_t_1_all >0, sig star(0.01) 

*Table 3- descriptive statistics (and extra correlations)
global controls_w size_w lnsize_w age_w lnage_w tobinsq_w roa_w tangibility_w xrd_at_w secrets5_w ind_std_cfo_at_w risk_length_w lnrisk_length_w read_w lnread_w inst_perblock_w ind_directors_w risk_com_w 
eststo clear
eststo All: estpost summarize $depvar $controls_w, detail 
esttab All using "C:\Table_3.csv", mtitles nogaps cells("    mean(pattern(1 1 1 0) fmt(2))   sd(pattern(1 1 1 0) fmt(2))     p1(pattern(1 1 1 0) fmt(2))  p25(pattern(1 1 1 0) fmt(2))       p50(pattern(1 1 1 0) fmt(2))     p75(pattern(1 1 1 0) fmt(2))     p99(pattern(1 1 1 0) fmt(2))     b(star pattern(0 0 0 1) fmt(2)) t(pattern(0 0 0 1) par fmt(2))") label replace
set more off
estpost correlate $depvar $controls_w, matrix listwise
esttab . using "C:\correlations.csv",b(%10.2f) unstack not noobs compress replace

*Table 4
global depvar2 sim_breaches_t_1_all
global regcontrols  lnsize_w  lnage_w tobinsq_w roa_w  tangibility_w xrd_at_w secrets5_w    
set more off
reg $depvar2 $regcontrols    ind_std_cfo_at_w   lnrisk_length_w lnread_w       inst_perblock_w ind_directors_w risk_com_w    i.fyear i.ind_ff12        , vce(cluster gvkey)
estimates store Model1_levels
set more off
areg $depvar2 $regcontrols    ind_std_cfo_at_w   lnrisk_length_w lnread_w       inst_perblock_w ind_directors_w risk_com_w    i.fyear         , absorb(gvkey) vce(cluster gvkey)
estimates store Model2_levels
esttab Model1_levels Model2_levels  using "C:\Table_4.csv", b(%10.3f) t wide   mtitles pr2 nogaps compress    noabbrev drop( )   star(* 0.10 ** 0.05 *** 0.01) scalars( )   sfmt(%10.0f  %10.0f  %10.0f %10.2f) wrap replace  order($controls2  $interactions)

* Table 5 Cybersecurity Risk and (Negative) Asymmetries in Stock Returns
*bring extra variables 
merge m:1 gvkey fyear using crash_cyberrisk.dta
keep if _merge==3
sort gvkey fyear
drop _merge
tsset gvkey fyear 
winsor2 ncskew extr_sigma, suffix(_w) cuts(1 99)
**models based on negative skewness
set more off
reg  ncskew_w L1.sim_breaches_t_1_all    $controls3      i.fyear           , vce(cluster gvkey)
estimates store Model1
**models based on exret sigma
set more off
reg  extr_sigma_w L1.sim_breaches_t_1_all    $controls3      i.fyear           , vce(cluster gvkey)
estimates store Model2
esttab Model1 Model2 using Table_5.csv, b(%10.3f) t    mtitles pr2 nogaps compress   noabbrev drop(*fyear)   nostar  scalars( )   sfmt(%10.0f  %10.0f  %10.0f %10.2f) wrap replace  order() 

*Table 6-Cybersecurity Risk and Future Cyberattacks
*standardization of variables
foreach var of varlist   sim_breaches_t_1_all   simj_breaches_t_1_all   lnsize_w lnage_w tobinsq_w roa_w tangibility_w xrd_at_w secrets5_w ind_std_cfo_at_w lnrisk_length_w lnread_w  inst_perblock_w   ind_directors_w  { 
bys fyear: egen  `var'_m=mean(`var')
bys fyear: egen `var'_sd=sd(`var') 
gen `var'_standard= (`var' - `var'_m)/ `var'_sd
drop `var'_m
drop `var'_sd
}
global mainvar1 sim_breaches_t_1_all_standard  
global controls3  lnsize_w_standard lnage_w_standard tobinsq_w_standard roa_w_standard tangibility_w_standard xrd_at_w_standard secrets5_w_standard ind_std_cfo_at_w_standard lnrisk_length_w_standard lnread_w_standard  inst_perblock_w_standard   ind_directors_w_standard  risk_com_w   
*all attacks
set more off
logit atleast1attack_1 $mainvar1 ind_ff12_dum*   i.fyear , vce(cluster gvkey) 
estimates store Model1
set more off
logit atleast1attack_1 $mainvar1  attack  $controls3             i.fyear , vce(cluster gvkey) 
estimates store Model2
***major attacks
set more off
logit atleast1major_attack_1  $mainvar1 ind_ff12_dum*   i.fyear , vce(cluster gvkey) 
estimaes store Model3
set more off
logit atleast1major_attack_1  $mainvar1  attack  $controls3    i.fyear , vce(cluster gvkey) 
estimates store Model4
*non major attacks
set more off
logit atleast1nonmajor_attack_1  $mainvar1 ind_ff12_dum*   i.fyear , vce(cluster gvkey) 
estimates store Model5
set more off
logit atleast1nonmajor_attack_1  $mainvar1  attack  $controls3    i.fyear , vce(cluster gvkey) 
estimates store Model6
esttab Model1 Model2 Model3 Model4 Model5 Model6 using Table6.csv, b(%10.3f) t    mtitles pr2 nogaps compress   noabbrev drop(*fyear  )   nostar  scalars( )   sfmt(%10.0f  %10.0f  %10.0f %10.2f) wrap replace  order() 

*Table 7 Portfolio results
*use the files monthly_returns.dta  and monthly_cyber_index.dta
*the variable timenew is a time indicator variable constructed as follows
generate timenew=.
forvalues   year = 2005/2019   {
forvalues  month =1/12 {
quietly replace timenew = (`year'- 2005)*12 + `month'    if  (year== `year'  & month ==`month')
}
}
*the variable  return_postrank is simply the F1.ret
clear all
use "C:\monthly_returns.dta", clear
tsset lpermno timenew
merge 1:1 lpermno timenew using "C:\monthly_cyber_index.dta" 
keep if _merge==3
drop _merge
*select sorting variable and number of portfolios
global sorting_var cyberrisk
global port 2
drop if return_postrank==.
winsor2 return_postrank, replace cuts(1 99) trim
preserve
gen cyberrisk_port_q =.
replace cyberrisk_port_q =0 if $sorting_var ==0 & $sorting_var!=.
drop if cyberrisk_port_q != 0
save p0, replace
clear
restore
preserve
drop if $sorting_var ==0
tsset lpermno timenew
bys lpermno quarternew: gen cyberrisk_q= $sorting_var[_N]
bys lpermno quarternew: gen timenew_q= timenew[_N]
drop timenew
rename timenew_q timenew
sort lpermno timenew
duplicates drop lpermno quarternew, force
bys quarternew:  egen cyberrisk_port_q=xtile(cyberrisk_q), nq($port)
keep lpermno quarternew  cyberrisk_q  cyberrisk_port_q
drop if cyberrisk_port_q==.
save quarterly_portfolios, replace
restore
drop if $sorting_var ==0
merge m:1 lpermno quarternew using "C:\quarterly_portfolios.dta" 
drop _merge
gen cyber_port= cyberrisk_port_q
order lpermno quarternew  year quarter month $sorting_var cyberrisk_q cyberrisk_port_q cyber_port
bys lpermno: replace cyber_port= cyber_port[_n-1] if missing(cyber_port)
sort lpermno timenew
rename cyber_port  cyberrisk_port_q
order lpermno date year quarter quarternew month timenew ret return_postrank shrcd exchcd siccd comnam shrcls dlstcd dlprc prc shrout mv firm_id dcik  dcik_2 gvkey fyear filingdate size $sorting_var cyberrisk_port_q
drop if cyberrisk_port_q==.
append using p0
*generate vw returns
bys  cyberrisk_port_q     timenew:  egen totalmv = total(mv)
generate weight = mv/ totalmv
gen vw_ret_not_cum = weight* return_postrank
bys cyberrisk_port_q timenew: egen vwret_p = total(vw_ret_not_cum)
*bring in extra data that we need from the file famamacbeth_1989_2019_w
merge m:1 lpermno year month using famamcbeth_1989_2019_w.dta
drop if timenew==. 
drop _merge
sort lpermno year month
bys  cyberrisk_port_q timenew:  egen ewret_p = mean (return_postrank)
bys  cyberrisk_port_q timenew:  gen num_stocks_p = _N
bys  cyberrisk_port_q timenew:  egen avg_lnmv_p = mean(lnmv)
bys  cyberrisk_port_q timenew:  egen avg_size_p = mean(size)
bys  cyberrisk_port_q timenew:  egen avg_bm_p = mean(bm)
bys  cyberrisk_port_q timenew:  egen avg_lnsize_p = mean(lnsize)
bys  cyberrisk_port_q timenew:  egen avg_roa_p = mean(roa)
bys  cyberrisk_port_q timenew:  egen avg_inst_perblock_p = mean(inst_perblock)
bys  cyberrisk_port_q timenew:  egen avg_amihud_illiq_p = mean(amihud_illiq)
bys  cyberrisk_port_q timenew:  egen avg_ivol_p = mean(ivol)
bys  cyberrisk_port_q timenew:  egen avg_lnread_p = mean(lnread)
bys  cyberrisk_port_q timenew:  egen avg_lnrisk_length_p = mean(lnrisk_length)
bys  cyberrisk_port_q timenew:  egen avg_cyberrisk_p = mean($sorting_var)
quietly bys   timenew  cyberrisk_port_q   :  gen dup = cond(_N==1,0,_n)
drop if dup >1
drop dup
keep year quarter month timenew cyberrisk_port_q vwret_p ewret_p  num_stocks avg_lnmv avg_size  avg_bm avg_lnsize avg_roa     avg_inst_perblock  avg_amihud_illiq  avg_ivol avg_lnread   avg_lnrisk_length  avg_cyberrisk 
drop if year==.
reshape wide  ewret_p vwret_p   num_stocks_p   avg_lnmv_p  avg_size_p  avg_bm_p avg_lnsize_p  avg_roa_p    avg_inst_perblock_p avg_amihud_illiq_p   avg_ivol_p avg_lnread_p     avg_lnrisk_length_p   avg_cyberrisk_p        , i(timenew) j( cyberrisk_port_q )  
order year quarter month timenew ew* vw*     num* avg_lnmv*  avg_size* avg_bm* avg_lnsize* avg_roa* avg_inst* avg_amih*  avg_ivol* avg_lnread* avg_lnrisk*  avg_cyberrisk*
*then add the 5-factors
*since the dependent variable is the postranking return, we need the factors to be postrank
merge 1:1 timenew using monthly_factors_adjusted.dta
keep if _merge==3
drop _merge
tsset timenew
gen ew_spread_$port_0= (ewret_p$port - ewret_p0)*100
gen vw_spread_$port_0 = (vwret_p$port - vwret_p0)*100
drop if ew_spread_$port_0 ==.
drop if vw_spread_$port_0 ==.
*drop if there are very few stocks per portfolio
drop if timenew <39
drop if timenew >171
foreach var of varlist   ewret_p0  ewret_p1  ewret_p$port  vwret_p0  vwret_p1    vwret_p$port   {
		gen excess_`var' = `var'*100    -rf
}
*average returns
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0  {
eststo: newey      `var'      , lag(12)            
		}
esttab  using avg_ret.csv, nostar replace
*capm
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0   {
eststo: newey      `var'     rmrf , lag(12)            
		}
esttab  using capm.csv, nostar replace
*ff3 factors
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0   {
eststo: newey      `var'     rmrf smb hml , lag(12)            
		}
esttab  using ff3.csv, nostar replace
*Carhart 4-factor
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0   {
eststo: newey      `var'     rmrf smb hml mom , lag(12)            
		}
esttab  using carhart.csv, nostar replace
*ff5
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0     {
eststo: newey      `var'     rmrf smb hml rmw cma , lag(12)            
		}
esttab  using ff5.csv, nostar replace

*Table 8 Double-Sorted Portfolios
*illustrative example using one variable (book to market) for the double-sorting. Similar code for other variables 
global sorting_var cyberrisk
global port 2
global var bm_w
global group _port_q
drop if return_postrank==.
winsor2 return_postrank, replace cuts(1 99) trim
preserve
gen cyberrisk_port_q =.
replace cyberrisk_port_q =0 if $sorting_var ==0 & $sorting_var!=.
drop if cyberrisk_port_q != 0
save p0, replace
clear
restore
preserve
drop if $sorting_var ==0
tsset lpermno timenew
bys lpermno quarternew: gen cyberrisk_q= cyberrisk[_N]
bys lpermno quarternew: gen timenew_q= timenew[_N]
drop timenew
rename timenew_q timenew
sort lpermno timenew
duplicates drop lpermno quarternew, force
bys quarternew:  egen cyberrisk_port_q=xtile(cyberrisk_q), nq($port)
keep lpermno quarternew  timenew cyberrisk_q  cyberrisk_port_q
drop if cyberrisk_port_q==.
save quarterly_portfolios, replace
restore
drop if $sorting_var ==0
merge m:1 lpermno timenew using "C:\quarterly_portfolios.dta" 
drop _merge
gen cyber_port= cyberrisk_port_q
order lpermno quarternew  year quarter month cyberrisk cyberrisk_q cyberrisk_port_q cyber_port
bys lpermno: replace cyber_port= cyber_port[_n-1] if missing(cyber_port)
sort lpermno timenew
drop cyberrisk_port_q
drop cyberrisk_q
rename cyber_port  cyberrisk_port_q
order lpermno date year quarter quarternew month timenew ret return_postrank shrcd exchcd siccd comnam shrcls dlstcd dlprc prc shrout mv firm_id dcik  dcik_2 gvkey fyear filingdate size_w cyberrisk cyberrisk_port_q
drop if cyberrisk_port_q==.
append using p0
*since we are interested in 2 groups, we use nq(2)
bys quarternew:  egen   $var$group =xtile($var), nq(2)
save tempfile, replace
foreach i in 1 2   {
clear all
use tempfile
keep if  $var$group  == `i'
*generate vw returns
bys  cyberrisk_port_q     timenew:  egen totalmv = total(mv)
generate weight = mv/ totalmv
gen vw_ret_not_cum = weight* return_postrank
bys cyberrisk_port_q timenew: egen vwret_p = total(vw_ret_not_cum)
*gen ew return
bys  cyberrisk_port_q timenew:  egen ewret_p = mean (return_postrank)
bys  cyberrisk_port_q timenew:  gen num_stocks_p = _N
bys  cyberrisk_port_q timenew:  egen avg_size_p = mean (size_w)
bys  cyberrisk_port_q timenew:  egen avg_cyberrisk_p = mean (cyberrisk)
bys  cyberrisk_port_q timenew:  egen avg_mv_p = mean (mv)
quietly bys   timenew  cyberrisk_port_q   :  gen dup = cond(_N==1,0,_n)
drop if dup >1
drop dup
keep year quarter month timenew cyberrisk_port_q vwret_p ewret_p    avg_size avg_mv   num_stocks avg_cyberrisk 
reshape wide  ewret_p vwret_p  avg_size_p  avg_mv_p    num_stocks_p  avg_cyberrisk_p    , i(timenew) j( cyberrisk_port_q )
order year quarter month timenew ew* vw* avg_size* avg_mv*  num_stocks* avg_cyberrisk*
preserve
merge 1:1 timenew using monthly_factors_adjusted.dta
keep if _merge==3
drop _merge
global sorting_var cyberrisk
global port 2
tsset timenew
gen ew_spread_$port_0= (ewret_p$port - ewret_p0)*100
gen vw_spread_$port_0 = (vwret_p$port - vwret_p0)*100
drop if ew_spread_$port_0 ==.
drop if vw_spread_$port_0 ==.
oreach var of varlist   ewret_p0  ewret_p1  ewret_p$port  vwret_p0  vwret_p1    vwret_p$port   {
		gen excess_`var' = `var'*100    -rf
}
*average returns
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0  {
eststo: newey      `var'      , lag(12)            
		}
esttab  using avg_ret_$var`i'.csv, nostar replace
*capm
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0   {
eststo: newey      `var'     rmrf , lag(12)            
		}
esttab  using capm$var`i'.csv, nostar replace
*ff3
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0   {
eststo: newey      `var'     rmrf smb hml , lag(12)            
		}
esttab  using ff3$var`i'.csv, nostar replace
*Carhart 4 factor
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0   {
eststo: newey      `var'     rmrf smb hml mom , lag(12)            
		}
esttab  using carhart$var`i'.csv, nostar replace
*ff5
eststo clear
foreach var of varlist      excess_ewret_p0  excess_ewret_p1  excess_ewret_p$port  ew_spread_$port_0   excess_vwret_p0   excess_vwret_p1         excess_vwret_p$port   vw_spread_$port_0     {
eststo: newey      `var'     rmrf smb hml rmw cma , lag(12)            
		}
esttab  using ff5$var`i'.csv, nostar replace
}

*Table 9
clear all
use "C:\monthly_returns.dta", clear
winsor2 return_postrank, replace cuts(1 99) trim
tsset lpermno timenew
merge 1:1 lpermno timenew using "C:\monthly_cyber_index.dta" 
keep if _merge==3
drop _merge
*bring in the data on beta, ivol and other variables required
merge m:1 lpermno year month  using famamcbeth_1989_2019_w.dta
drop _merge
global sorting_var cyberrisk
drop if return_postrank==.
drop if $sorting_var ==.
merge m:1 year month using "C:\ rf.dta"
keep if _merge==3
drop _merge
order date- return_postrank return_postrank* 
*create excess returns
foreach x of varlist    return_postrank return_postrank2 return_postrank3 return_postrank4  return_postrank5 return_postrank6  return_postrank9  return_postrank12     {
gen excess_`x'= (`x'*100)-rf

}
*stabdardize variables
foreach x of varlist     cyberrisk   cyberriskj  reversal lnmv  bm ivol   lnprice roa     asset_growth  beta_3year  beta_5year amihud_illiq mom bali_max coskew   lnread  lnrisk_length {
egen mean_`x' = mean(`x'), by(timenew)
egen std_`x' = sd(`x'), by(timenew)
gen `x'_standard = (`x' - mean_`x')/std_`x'

drop mean_`x' std_`x'
}
*fama macbeth regressions
set more off
asreg excess_return_postrank cyberrisk_standard     , fmb newey(4)
estimates store Model1
set more off
asreg excess_return_postrank cyberrisk_standard  beta_3year_standard   lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard , fmb newey(4)
estimates store Model2
set more off
asreg excess_return_postrank cyberrisk_standard   beta_3year_standard    lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard  lnrisk_length_w_standard  lnread_w_standard   , fmb newey(4)
estimates store Model3
set more off
asreg excess_return_postrank2 cyberrisk_standard   beta_3year_standard    lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard  lnrisk_length_w_standard  lnread_w_standard   , fmb newey(4)
estimates store Model4
set more off
asreg excess_return_postrank3 cyberrisk_standard   beta_3year_standard    lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard  lnrisk_length_w_standard  lnread_w_standard   , fmb newey(4)
estimates store Model5
set more off
asreg excess_return_postrank6 cyberrisk_standard   beta_3year_standard    lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard  lnrisk_length_w_standard  lnread_w_standard   , fmb newey(4)
estimates store Model6
set more off
asreg excess_return_postrank9 cyberrisk_standard   beta_3year_standard    lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard  lnrisk_length_w_standard  lnread_w_standard   , fmb newey(4)
estimates store Model7
set more off
asreg excess_return_postrank12 cyberrisk_standard   beta_3year_standard    lnmv_standard    bm_w_standard    mom_standard  reversal_standard amihud_illiq_standard coskew_standard  ivol_standard  asset_growth_w_standard roa_w_standard bali_max_standard  lnrisk_length_w_standard  lnread_w_standard   , fmb newey(4)
estimates store Model8

*Table 10
clear all
use "C:\factor_rfs_revision.dta"
tsset day_id
set more off
newey vw_factor_5_groups_p1_zero all_12, lag(6) 
estimates store Model1
set more off
newey vw_factor_5_groups_p1_zero all_12 rmrf, lag(6) 
estimates store Model2
set more off
newey vw_factor_5_groups_p1_zero all_12 rmrf smb hml mom, lag(6) 
estimates store Model3
set more off
newey vw_factor_5_groups_p1_zero all_12 rmrf smb hml rmw cma, lag(6)
estimates store Model4
set more off
newey vw_factor_10_groups_p1_zero all_12, lag(6) 
estimates store Model5
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf, lag(6) 
estimates store Model6
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf smb hml mom, lag(6) 
estimates store Model7
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf smb hml rmw cma, lag(6)
estimates store Model8
*alternative shock 1 
clear 
use "C:\ factor2_rfs_revision_temp30d.dta"
tsset day_id
set more off
newey vw_factor_10_groups_p1_zero all_12, lag(6) 
estimates store Model9
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf, lag(6) 
estimates store Model10
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf smb hml mom, lag(6) 
estimates store Model11
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf smb hml rmw cma, lag(6)
estimates store Model12
*alternative shock 2 
clear 
use "C:\ factor2_rfs_revision_14d.dta"
tsset day_id
set more off
newey vw_factor_10_groups_p1_zero all_12, lag(6) 
estimates store Model13
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf, lag(6) 
estimates store Model14
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf smb hml mom, lag(6) 
estimates store Model15
set more off
newey vw_factor_10_groups_p1_zero all_12 rmrf smb hml rmw cma, lag(6)
estimates store Model16
*placebo tests
use "C:\factor2_rfs_revision_14dplacebo.dta"
set more off
newey vw_factor_5_groups_p1_zero  shock_placebo5, lag(6)
estimates store Model17
set more off
newey vw_factor_5_groups_p1_zero  shock_placebo5 rmrf  , lag(6)
estimates store Model18
set more off
newey vw_factor_5_groups_p1_zero shock_placebo5      rmrf  smb hml mom, lag(6)
estimates store Model19
newey vw_factor_5_groups_p1_zero shock_placebo5     rmrf  smb hml rmw cma, lag(6)
estimates store Model20
set more off
newey vw_factor_5_groups_p1_zero shock_placebo23, lag(6)
estimates store Model21
set more off
newey vw_factor_5_groups_p1_zero shock_placebo23 rmrf  , lag(6)
estimates store Model22
set more off
newey vw_factor_5_groups_p1_zero shock_placebo23     rmrf  smb hml mom, lag(6)
estimates store Model23
newey vw_factor_5_groups_p1_zero shock_placebo23    rmrf  smb hml rmw cma, lag(6)
estimates store Model24
esttab Model1 Model2 Model3 Model4 Model5 Model6 Model7 Model8 Model9 Model10 Model11 Model12 Model13 Model14 Model15 Model16 Model17 Model18 Model19 Model20   Model21  Model22  Model23  Model24  using Table_10.csv, b(%10.4f) t    mtitles pr2 nogaps compress   noabbrev    nostar  scalars( )   sfmt(%10.0f  %10.0f  %10.0f %10.3f) wrap replace  order()

**Table 11
* Initial data-CARs in different windows
use "C:\CARS.dta" , clear
foreach var of varlist   sim_breaches_t_1_all    lnsize_w lnage_w tobinsq_w roa_w tangibility_w xrd_at_w secrets5_w ind_std_cfo_at_w lnrisk_length_w lnread_w  inst_perblock_w   ind_directors_w  { 
bys fyear: egen  `var'_m=mean(`var')
bys fyear: egen `var'_sd=sd(`var') 
gen `var'_standard= (`var' - `var'_m)/ `var'_sd
drop `var'_m
drop `var'_sd
}
global controls2  lnsize_w_standard lnage_w_standard tobinsq_w_standard roa_w_standard tangibility_w_standard xrd_at_w_standard secrets5_w_standard ind_std_cfo_at_w_standard lnrisk_length_w_standard lnread_w_standard  inst_perblock_w_standard   ind_directors_w_standard  risk_com_w   
egen cyber_group3=cut( sim_breaches_t_1_all), group(3)
tab cyber_group3, gen(cyber_group3_)
egen cyber_group5=cut( sim_breaches_t_1_all), group(5)
tab cyber_group5, gen(cyber_group5_)
egen cyber_group10=cut( sim_breaches_t_1_all), group(10)
tab cyber_group10, gen(cyber_group10_)
local i = 0
foreach var of varlist   CAR_Window_1 CAR_Window_2 CAR_Window_4   { 
local i = `i' + 1
set more off
reg     `var'    sim_breaches_t_1_all 
estimates store Model`i'
}
local i = 3
foreach var of varlist   CAR_Window_1 CAR_Window_2 CAR_Window_4    { 
local i = `i' + 1
set more off
reg     `var'  cyber_group5_5 
estimates store Model`i'
}
local i = 6
foreach var of varlist   CAR_Window_1 CAR_Window_2 CAR_Window_4    { 
local i = `i' + 1
set more off
reg     `var'  cyber_group10_9
estimates store Model`i'
}
esttab Model1 Model2 Model3 Model4 Model5  Model6  Model7 Model8 Model9    using Table_11.csv, b(%10.3f) t    mtitles pr2 nogaps compress   noabbrev     nostar  scalars( )   sfmt(%10.0f  %10.0f  %10.0f %10.2f) wrap replace  order() 

*Table 12
*need to merge the file with the CARS and the file with all connected firms (swi_connected), as identified in Factset Revere
clear all
use "C:\CARS.dta" , clear
rename GVKEY gvkey
rename FYEAR fyear
rename TIC ticker
destring gvkey, replace
destring sic, replace
set more off
egen stratum1 = group(fyear ind_ff12)
tabulate ind_ff12, generate(ind_ff12_dum)
tabulate fyear, generate(fyear_dum)
tabulate stratum1, generate(stratum1_dum)
*checked uniqueness of tickers in the sample
merge m:1 ticker using swi_connected
drop if fyear==.
replace connected=0 if connected==. & CAR_Window_1 !=.
drop _merge
*add data on abnormal institutional investor attention
merge m:1 gvkey using aia_data
drop _merge
order gvkey fyear connected- aia_dec18
gen high_aia=0
replace high_aia =1 if aia_dec14>2 | aia_dec15>2  | aia_dec16 >2 | aia_dec17>2 | aia_dec18>2
replace high_aia =. if  aia_dec14==.  | aia_dec15 ==.  | aia_dec16 ==. | aia_dec17==. | aia_dec18==.
ttest high_aia, by (connected)
ttest CAR_Window_1, by(connected) 
ttest CAR_Window_2, by(connected) 
ttest CAR_Window_4, by(connected)
ttest sim_breaches_t_1_all , by(connected) 
ttest high_aia, by (connected)
ttest CAR_Window_1, by(connected) unequal
ttest CAR_Window_2, by(connected) unequal
ttest CAR_Window_4, by(connected) unequal
ttest sim_breaches_t_1_all , by(connected) unequal
egen cyber_group3=cut( sim_breaches_t_1_all), group(3)
tab cyber_group3, gen(cyber_group3_)
egen cyber_group10=cut( sim_breaches_t_1_all), group(10)
tab cyber_group10, gen(cyber_group10_)
set more off
logit connected sim_breaches_lag1_standard $controls
estimates store Model1
set more off
logit connected cyber_group3_3 $controls
estimates store Model2
set more off
logit connected cyber_group10_9 $controls 
estimates store Model3
esttab Model1 Model2 Model3  using Table_12.csv, b(%10.3f) t    mtitles pr2 nogaps compress   noabbrev     nostar  scalars( )   sfmt(%10.0f  %10.0f  %10.0f %10.2f) wrap replace  order() 
